package teacher.service;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import beanDao.MapToBean;

import teacher.bean.Award;
import teacher.bean.Paper;
import teacher.bean.Patent;
import teacher.bean.Treatise;
import teacher.bean.Teacher;
import teacher.page.Page;
import tools.StringDate;
import database.dao.MysqlCrud;

public class treatise {
	
	/**
	 * @category 用于论著信息的修改
	 * @param treatise 修改的内容
	 * @param a 定位修改的位置
	 * @return 修改成功，返回true，否则返回flase
	 */
	public boolean updateTreatise(Treatise treatise,int a) {
		String sql= "update treatise set treId=?,name=?,author=?,date=?,state=?,class=?,press=?,abstract=?,upload=?,bookNum=?,teaId=? where treId="+a+"";
		String[] parameter = {
				String.valueOf(treatise.getTreId()),
				String.valueOf(treatise.getName()),
				String.valueOf(treatise.getAuthor()),
				String.valueOf(new StringDate().DateToString(treatise.getDate())),
				String.valueOf(treatise.getState()),
				String.valueOf(treatise.getClass_()),
				String.valueOf(treatise.getPress()),
				String.valueOf(treatise.getAbstract_()),
				String.valueOf(treatise.getUpload()),
				String.valueOf(treatise.getBookNum()),
				String.valueOf(treatise.getTeaId())
		};
		MysqlCrud mysqlcurd=new MysqlCrud();
		boolean flag= mysqlcurd.InsertAndUpdateData(sql, parameter);
		return flag;
	}
	
	/**
	 * @category 通过时间段查询老师论著
	 * @return paperOne List<Treatise> 返回查询到的专利列表
	 */
	public List<Treatise> selectTimeTreatiseTea(Teacher t,Date date1,Date date2) {
		String sql= "select * from (select * from treatise order by state,class,date desc) as acheve where acheve.date>='"+date1+"' and acheve.date<='"+date2+"' and acheve.teaId=?";
		int i=0;
		List<Treatise> treatiseOne=new ArrayList<Treatise>();
		MysqlCrud mysqlcurd=new MysqlCrud();
		String[] parameter={String.valueOf(t.getTeaId())};
		List<Map> list = mysqlcurd.SelectData(sql, parameter);
		//System.out.println(list.size());
		while(i<list.size()){
			Map map=list.get(i);
			treatiseOne.add(new MapToBean().MapToTreatise(map));
			i++;
		}
		return treatiseOne;
	}
	
	/**
	 * @category 通过时间段查询老师论著分页
	 * @return paperOne List<Treatise> 返回查询到的专利列表
	 */
	public List<Treatise> selectTimeTreatiseTeaPage(Teacher t,Date date1,Date date2,Page page) {
		String sql= "select * from (select * from treatise order by state,class,date desc) as acheve where acheve.date>='"+date1+"' and acheve.date<='"+date2+"' and acheve.teaId=? limit ?,?";
		int i=0;
		List<Treatise> treatiseOne=new ArrayList<Treatise>();
		MysqlCrud mysqlcurd=new MysqlCrud();
		Integer[] parameter={t.getTeaId(),page.getBeginIndex(),page.getEveryPage()};
		List<Map> list = mysqlcurd.SelectDataInt(sql, parameter);
		//System.out.println(list.size());
		while(i<list.size()){
			Map map=list.get(i);
			treatiseOne.add(new MapToBean().MapToTreatise(map));
			i++;
		}
		return treatiseOne;
	}
	
	/**
	 * @category 通过老师查询论著
	 * @param tea Teacher 老师信息
	 * @return paperOne List<Paper> 返回查询到的论文列表
	 */
	public List<Treatise> selectTreatiseByTeacher(Teacher tea) {
		String sql= "select * from (select * from treatise order by state,class,date desc) as acheve,teacher as t where acheve.teaId = t.teaId and t.user = ?";
		int i=0;
		List<Treatise> treatiseOne=new ArrayList<Treatise>();
		MysqlCrud mysqlcurd=new MysqlCrud();
		String[] parameter={ String.valueOf(tea.getUser())};
		List<Map> list = mysqlcurd.SelectData(sql, parameter);
		//System.out.println(list.size());
		while(i<list.size()){
			Map map=list.get(i);
			treatiseOne.add(new MapToBean().MapToTreatise(map));
			i++;
		}
		return treatiseOne;
	}
	
	/**
	 * @category 通过论著ID查询一项论著
	 * @return paperOne List<Paper> 返回查询到的论文列表
	 */
	public Treatise selectTreatiseById(int a) {
		Treatise t = new Treatise();
		String sql= "select * from treatise where treId=?";
		int i=0;	
		MysqlCrud mysqlcurd=new MysqlCrud();
		String[] parameter={String.valueOf(a)};
		List<Map> list = mysqlcurd.SelectData(sql, parameter);
		//System.out.println(list.size());
		if(1==list.size()){
			Map map=list.get(0);
			t=new MapToBean().MapToTreatise(map);
		
		}
		return t;
	}
	
	/**
	 * @category 通过老师查询论著分页
	 * @param tea Teacher 老师信息
	 * @return paperOne List<Paper> 返回查询到的论文列表
	 */
	public List<Treatise> selectTreatiseByTeacherPage(Teacher t1,Page page) {
		String sql= "select * from (select * from treatise order by state,class,date desc) as acheve where acheve.teaId=? limit ?,?";
		int i=0;
		List<Treatise> treatiseOne=new ArrayList<Treatise>();
		MysqlCrud mysqlcurd=new MysqlCrud();
		Integer[] parameter={t1.getTeaId(),page.getBeginIndex(),page.getEveryPage()};
		List<Map> list = mysqlcurd.SelectDataInt(sql, parameter);
		//System.out.println(list.size());
		while(i<list.size()){
			Map map=list.get(i);
			treatiseOne.add(new MapToBean().MapToTreatise(map));
			i++;
		}
		return treatiseOne;
	}
	
	/**
	 * @category 查询所有论著
	 * @return paperOne List<Paper> 返回查询到的论文列表
	 */
	public List<Treatise> selectAllTreatise() {
		String sql= "select * from (select * from treatise order by state,class,date desc) as acheve where (acheve.state=4 or acheve.state=5)";
		int i=0;
		List<Treatise> treatiseOne=new ArrayList<Treatise>();
		MysqlCrud mysqlcurd=new MysqlCrud();
		List<Map> list = mysqlcurd.SelectData(sql, null);
		//System.out.println(list.size());
		while(i<list.size()){
			Map map=list.get(i);
			treatiseOne.add(new MapToBean().MapToTreatise(map));
			i++;
		}
		return treatiseOne;
	}
	/**
	 * @category 统计论著的数量
	 * @return sum int 论著的数量
	 */
		public int getCountTreatise()
		{
			int sum=0;
			String sql="select count(*) from treatise where state>3";
			MysqlCrud mysqlcurd=new MysqlCrud();
			List<Map> lists=mysqlcurd.SelectData(sql, null);
			Map map=lists.get(0);
			sum=Integer.parseInt(map.get(0).toString());
			return sum;
		}
	
		/**
		 * @category 通过字段模糊查询论著
		 * @param vague 模糊查询需要字段
		 * @return treatiseOne List<Treatise> 返回查询到的论著列表
		 */
		public List<Treatise> selectVagueTreatise(String vague) {
			String sql= "select * from (select * from treatise order by state,class,date desc) as acheve where (acheve.state=4 or acheve.state=5) and acheve.name like '%"+vague+"%'";
			int i=0;
			List<Treatise> treatiseOne=new ArrayList<Treatise>();
			MysqlCrud mysqlcurd=new MysqlCrud();
			List<Map> list = mysqlcurd.SelectData(sql, null);
			//System.out.println(list.size());
			while(i<list.size()){
				Map map=list.get(i);
				treatiseOne.add(new MapToBean().MapToTreatise(map));
				i++;
			}
			return treatiseOne;
		}
		
		/**
		 * @category 通过字段模糊查询老师论著
		 * @param vague 模糊查询需要字段
		 * @return treatiseOne List<Treatise> 返回查询到的论著列表
		 */
		public List<Treatise> selectVagueTreatiseTea(String vague,Teacher t1) {
			String sql= "select * from (select * from treatise order by state,class,date desc) as acheve where acheve.name like '%"+vague+"%' and acheve.teaId=?";
			int i=0;
			List<Treatise> treatiseOne=new ArrayList<Treatise>();
			MysqlCrud mysqlcurd=new MysqlCrud();
			String[] parameter={ String.valueOf(t1.getTeaId())};
			List<Map> list = mysqlcurd.SelectData(sql, parameter);
			//System.out.println(list.size());
			while(i<list.size()){
				Map map=list.get(i);
				treatiseOne.add(new MapToBean().MapToTreatise(map));
				i++;
			}
			return treatiseOne;
		}
		
		/**
		 * @category 通过字段模糊查询老师论著分页
		 * @param vague 模糊查询需要字段
		 * @return treatiseOne List<Treatise> 返回查询到的论著列表
		 */
		public List<Treatise> selectVagueTreatiseTea(String vague,Teacher t1,Page page) {
			String sql= "select * from (select * from treatise order by state,class,date desc) as acheve where acheve.name like '%"+vague+"%' and acheve.teaId=? limit ?,?";
			int i=0;
			List<Treatise> treatiseOne=new ArrayList<Treatise>();
			MysqlCrud mysqlcurd=new MysqlCrud();
			Integer[] parameter={ t1.getTeaId(),page.getBeginIndex(),page.getEveryPage()};
			List<Map> list = mysqlcurd.SelectDataInt(sql, parameter);
			//System.out.println(list.size());
			while(i<list.size()){
				Map map=list.get(i);
				treatiseOne.add(new MapToBean().MapToTreatise(map));
				i++;
			}
			return treatiseOne;
		}
		
		/**
		 * @category 通过时间段查询论著
		 * @return treatiseOne List<Treatise> 返回查询到的论著列表
		 */
		public List<Treatise> selectTimeTreatise(Date date1,Date date2) {
			String sql= "select * from (select * from treatise order by state,class,date desc) as acheve where (acheve.state=4 or acheve.state=5) and acheve.date>='"+date1+"' and acheve.date<='"+date2+"'";
			int i=0;
			List<Treatise> treatiseOne=new ArrayList<Treatise>();
			MysqlCrud mysqlcurd=new MysqlCrud();
			List<Map> list = mysqlcurd.SelectData(sql, null);
			//System.out.println(list.size());
			while(i<list.size()){
				Map map=list.get(i);
				treatiseOne.add(new MapToBean().MapToTreatise(map));
				i++;
			}
			return treatiseOne;
		}
		
		/**
		 * @category 插入一条论著数据
		 * @param treatise 所要插入的论著数据
		 * @return 插入成功返回true，否则返回false
		 */
		public boolean insertTreatise(Treatise treatise){
			boolean flag = false;
			String sql="insert into treatise(name,author,date,state,class,press,abstract,upload,bookNum,teaId)"+" values(?,?,?,?,?,?,?,?,?,?)";
			String[] parameter={
					String.valueOf(treatise.getName()),
					String.valueOf(treatise.getAuthor()),
					String.valueOf(treatise.getDate()),
					String.valueOf(treatise.getState()),
					String.valueOf(treatise.getClass_()),
					String.valueOf(treatise.getPress()),
					String.valueOf(treatise.getAbstract_()),
					String.valueOf(treatise.getUpload()),
					String.valueOf(treatise.getBookNum()),
					String.valueOf(treatise.getTeaId())
			};
			MysqlCrud mysqlcurd=new MysqlCrud();
			flag = mysqlcurd.InsertAndUpdateData(sql, parameter);
			return flag;
		}
		/**
		 * @category 统计一个老师论著的数量
		 * @return sum int 论著的数量
		 */
			public int getCountTreatiseTea(Teacher tea)
			{
				int sum=0;
				String[] parameter={String.valueOf(tea.getTeaId())};
				String sql="select count(*) from treatise where teaId=?";
				MysqlCrud mysqlcurd=new MysqlCrud();
				List<Map> lists=mysqlcurd.SelectData(sql, parameter);
				Map map=lists.get(0);
				sum=Integer.parseInt(map.get(0).toString());
				return sum;
			}
			
			/**
			 * @category 统计一个老师按照模糊查询论著的数量
			 * @return sum int 论著的数量
			 */
				public int getCountTreatiseTeaVague(String vague,Teacher t)
				{
					int sum=0;
					String[] parameter={String.valueOf(t.getTeaId())};
					String sql="select count(*) from treatise where name like '%"+vague+"%' and teaId=?";
					MysqlCrud mysqlcurd=new MysqlCrud();
					List<Map> lists=mysqlcurd.SelectData(sql, parameter);
					Map map=lists.get(0);
					sum=Integer.parseInt(map.get(0).toString());
					return sum;
				}
			
			/**
			 * @category 统计一个老师一定时间论著的数量
			 * @return sum int 论著的数量
			 */
				public int getCountTreatiseTeaTime(Teacher tea,Date date1,Date date2)
				{
					int sum=0;
					String[] parameter={String.valueOf(tea.getTeaId())};
					String sql="select count(*) from treatise where date>='"+date1+"' and date<='"+date2+"' and teaId=?";
					MysqlCrud mysqlcurd=new MysqlCrud();
					List<Map> lists=mysqlcurd.SelectData(sql, parameter);
					Map map=lists.get(0);
					sum=Integer.parseInt(map.get(0).toString());
					return sum;
				}
			/**
			 * @category 用于删除论著的信息
			 * @return 删除成功返回true，否则为false
			 */
			public boolean DeleteTre(Treatise treatise){
				String sql = "delete from treatise where treId=?";
				String[]  parameter={String.valueOf(treatise.getTreId())};
				 MysqlCrud mysqlcurd=new MysqlCrud();
				 boolean flag= mysqlcurd.InsertAndUpdateData(sql, parameter);
				 return flag;
			}

}
